package com.config;

import com.service.file.StorageServiceImpl;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletRegistration;

public class SpittrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    /**
     * spring配置
     */
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[]{SpringConfig.class, DBConfig.class, RedisConfig.class};

    }

    /**
     * springmvc配置
     */
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{WebmvcConfig.class};
    }

    /**
     * 拦截/
     */
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
    private static final long MAX_FILE_SIZE = 2097152000L; // 5MB : Max file size.
    // Beyond that size spring will throw exception.
//    private static final long MAX_REQUEST_SIZE = 5242880‬; // 20MB : Total request size containing Multi part.
    private static final long MAX_REQUEST_SIZE = 2097152000L;
    private static final int FILE_SIZE_THRESHOLD = 0; // Size threshold after which files will be written to disk
    @Override
    protected void customizeRegistration(ServletRegistration.Dynamic registration) {

        // Optionally also set maxFileSize, maxRequestSize, fileSizeThreshold
        MultipartConfigElement multipartConfigElement = new MultipartConfigElement(StorageServiceImpl.LOCATION, MAX_FILE_SIZE, MAX_REQUEST_SIZE, FILE_SIZE_THRESHOLD);

        registration.setMultipartConfig(multipartConfigElement);
    }
    ////    @Override
////    protected Filter[] getServletFilters() {
////        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
////        characterEncodingFilter.setEncoding("UTF-8");
////        characterEncodingFilter.setForceEncoding(true);
////        return new Filter[]{characterEncodingFilter/*, new OpenEntityManagerInViewFilter()*/};
////    }

}
